#!/bin/bash
# getretroarch.sh - Install RetroArch on GNU+Linux
#
# Written in 2017 by Caleb Herbert <csh@bluehome.net>
#
# To the extent possible under law, the author(s) have dedicated all
# copyright and neighboring rights to this software to the public
# domain worldwide.  This software is distributed without any
# warranty.
#
# You should have received a copy of the CC0 Public Domain Dedication
# along with this software.  If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.

	  # # # # # # # # # # # # # # # # # # # # # # # # #
	  #                    WARNING:                   #
	  #  This script is shit and contains Bashisms.   #
	  # # # # # # # # # # # # # # # # # # # # # # # # #

	  # # # # # # # # # # # # # # # # # # # # # # # # #
	  #              RUNNING THIS SCRIPT              #
	  #                                               #
	  #  To run this script on a GNU+Linux system,    #
	  #  make it executable and run it from the       #
	  #  terminal.                                    #
	  #                                               #
	  #    $ chmod +x getretroarch.sh                 #
	  #    $ ./getretroarch.sh                        #
	  #                                               #
          #  You may not need to do this if you got it    #
          #  in the recommended .tar.gz file.             #
          #                                               #
	  # # # # # # # # # # # # # # # # # # # # # # # # #

PROGRAM_ID="Get RetroArch"
SYSTEM=x86_64-linux
ME=$(whoami)
GNU_URI="https://www.gnu.org/software/guix/manual/html_node/Binary-Installation.html"
PACKAGE="retroarch"

win() {
    zenity --"$1" \
	   --title="$PROGRAM_ID" \
	   --text="$2"
}

uhoh() {
    spd-say "Uh-oh"
}

# Get Guix
pt1() {
    # Get Guix
    wget ftp://alpha.gnu.org/gnu/guix/guix-binary-0.13.0.$SYSTEM.tar.xz
    wget ftp://alpha.gnu.org/gnu/guix/guix-binary-0.13.0.$SYSTEM.tar.xz.sig

    # Get developer ID
    gpg --keyserver pgp.mit.edu --recv-keys 3CE464558A84FDC69DB40CFB090B11993D9AEBB5

    # Authenticate Guix
    gpg --verify guix-binary-0.13.0.$SYSTEM.tar.xz.sig
}

# Create storage for packages
pt2() {
gksudo bash <<EOF
    tar --warning=no-timestamp -xf \
	guix-binary-0.13.0.$SYSTEM.tar.xz
    mv var/guix /var/ && mv gnu /
EOF
}

pt3() {
gksudo bash <<EOF
    ln -sf /var/guix/profiles/per-user/root/guix-profile \
	~root/.guix-profile

    GUIX_PROFILE=$HOME/.guix-profile \
	source $GUIX_PROFILE/etc/profile
EOF
}

# Create group and user accounts for build users
pt4() {
gksudo bash <<EOF
    groupadd --system guixbuild
    for i in `seq -w 1 10`;
    do
	useradd -g guixbuild -G guixbuild           \
            -d /var/empty -s `which nologin`    \
            -c "Guix build user $i" --system    \
            guixbuilder$i;
    done
EOF
}

	  # # # # # # # # # # # # # # # # # # # # # # # # #
          #                   WAIT!                       #
          #  Um... Which init system are you running?     #
	  # # # # # # # # # # # # # # # # # # # # # # # # #

# Run Guix daemon
pt5() {
gksudo bash <<EOF
    # BASHISM!!! # if [ "$(/sbin/init --version)" =~ upstart ]
    # if [ "$(/sbin/init --version)" =~ upstart ]
    # Instead, use:
    if $(/sbin/init --version | grep upstart > /dev/null)
    then
	your_init_system="upstart"
    elif $(systemctl | grep '-\.mount' > /dev/null)
	your_init_system="systemd"
    elif     
	your_init_system="sysv"
    else
	your_init_system="idk"
	# Not used; IDK how to end 'if'.
    fi
    # Adapted from https://unix.stackexchange.com/a/164092

    if [ $your_init_system = "systemd" ]
    then
	cp ~root/.guix-profile/lib/systemd/system/guix-daemon.service \
	    /etc/systemd/system/
	systemctl start guix-daemon && systemctl enable guix-daemon
    elif [ $your_init_system = "upstart" ]
    then
	initctl reload-configuration
	cp ~root/.guix-profile/lib/upstart/system/guix-daemon.conf \
	    /etc/init/
	start guix-daemon
    else
	~root/.guix-profile/bin/guix-daemon \
	    --build-users-group=guixbuild
    fi
EOF
}

pt6() {
gksudo bash <<EOF
    # Provide "guix" command
    mkdir -p /usr/local/bin
    cd /usr/local/bin
    ln -s /var/guix/profiles/per-user/root/guix-profile/bin/guix

    # Provide Guix Info manual
    mkdir -p /usr/local/share/info
    cd /usr/local/share/info
    for i in /var/guix/profiles/per-user/root/guix-profile/share/info/* ;
    do ln -s $i ; done
EOF
}

pt7() {
gksudo bash <<EOF
    # Authorize binary packages/"substitutes"
    guix archive --authorize < ~root/.guix-profile/share/guix/hydra.gnu.org.pub
EOF
}
# All commands taken from
# https://www.gnu.org/software/guix/manual/html_node/Binary-Installation.html

pt8() {
    # Install RetroArch!
    guix package --install $PACKAGE
}

# The actions
main() {
    pt1        || win warning "Part 1"
    pt2 || win warning "Failed to create GNU store."
    pt3 || win warning "Failed step 3 in $GNU_URI"
    pt4 || win warning "Failed to create build users."
    pt5 || win warning "Failed to start Guix daemon."
    pt6 || win warning "Failed to provide guix and info manual."
    pt7 || win warning "Failed to authorize binary package substitutes."
    pt8        || win warning "Failed to install $PACKAGE."
}

# The GUI
cd /tmp

if win question "Would you like me to install RetroArch for you?"
then
    win warning \
	"Don't shut off your computer until you see another dialog box from me, OK?  Don't worry, this shouldn't take more than half an hour."
    main || win error "Uh-oh, I'm not working right."
else
    win notification "Done!"
fi

exit
